﻿#region Using Statemens
using System;
using GameBase.Screens;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StarFox.ScreenComponents;
#endregion

namespace StarFox.Screens
{
    /// <summary>
    /// Developer warning screen of the game.
    /// </summary>
    public class DevWarningScreen : StarFoxScreen
    {
        #region Initialization

        /// <summary>
        /// Initializes a new instance of this class.
        /// </summary>
        /// <param name="screenManager">The ScreenManager that this Screen is associated to.</param>
        public DevWarningScreen(ScreenManager screenManager)
            : base(screenManager)
        {
            Name = "DevWarningScreen";
        }

        /// <summary>
        /// Called when the Screen needs to be initialized.
        /// </summary>
        public override void Initialize()
        {
            Components2D.Add(new DevWarningMessage(this));

            base.Initialize();
        }

        #endregion
    }

    #region Developer Warning Message

    /// <summary>
    /// Developer warning message.
    /// </summary>
    public class DevWarningMessage : ScreenComponent2D
    {
        #region Fields

        // sprite font
        SpriteFont smallFont;

        // developer warning message
        string[] msgText = { "This is a trial version of a game still in development.",
                             "Please be aware that there is a chance that",
                             "minor bugs may be present.",   
                             "Thank you for your understanding and enjoy the game." };
        Vector2 msgPos;
        bool toDVW = false;
        readonly TimeSpan totalDVWTime = TimeSpan.FromMilliseconds(3000);

        #endregion

        #region Screen Transitions

        bool toVisible = false;
        bool toHidden = false;

        float trTime = 0.0f;

        /// <summary>
        /// Makes the ScreenComponent2D transition to visible state.
        /// </summary>
        public override void Show()
        {
            toVisible = true;
        }

        /// <summary>
        /// Makes the ScreenComponent2D transition to hidden state.
        /// </summary>
        public override void Hide()
        {
            toHidden = true;
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of this class.
        /// </summary>
        /// <param name="screen">The Screen that this ScreenComponent2D is associated to.</param>
        public DevWarningMessage(StarFoxScreen screen)
            : base(screen)
        {
            // asset loading
            smallFont = Game.Content.Load<SpriteFont>(ContentSettings.GetAsset("FONT_SMALL"));
        }

        #endregion

        #region Update

        /// <summary>
        /// Called when the ScreenComponent2D needs to be updated.
        /// </summary>
        /// <param name="gameTime">Time elapsed since the last call to this method.</param>
        public override void Update(GameTime gameTime)
        {
            #region Screen Transition

            if (toVisible)
            {
                // update transition timer
                float trDelta = (float)(gameTime.ElapsedGameTime.TotalMilliseconds / Time.TotalMilliseconds);
                trTime += trDelta;
                trTime = MathHelper.Clamp(trTime, 0.0f, 1.0f);
                if (trTime == 1.0f) { toVisible = false; toDVW = true; trTime = 0.0f; }
            }
            else if (toHidden)
            {
                // update transition timer
                float trDelta = (float)(gameTime.ElapsedGameTime.TotalMilliseconds / Time.TotalMilliseconds);
                trTime += trDelta;
                trTime = MathHelper.Clamp(trTime, 0.0f, 1.0f);
                if (trTime == 1.0f) { toHidden = false; trTime = 0.0f; }
            }

            #endregion

            #region DVW

            if (toDVW)
            {
                // update DVW timer
                float trDelta = (float)(gameTime.ElapsedGameTime.TotalMilliseconds / totalDVWTime.TotalMilliseconds);
                trTime += trDelta;
                trTime = MathHelper.Clamp(trTime, 0.0f, 1.0f);
                if (trTime == 1.0f) { toDVW = false; trTime = 0.0f; Screen.ScreenManager.AddScreen(new DreamBuildPlayScreen(Screen.ScreenManager)); }
            }

            #endregion
        }

        #endregion

        #region Draw

        /// <summary>
        /// Called when the ScreenComponent2D needs to be drawn.
        /// </summary>
        /// <param name="gameTime">Time elapsed since the last call to this method.</param>
        public override void Draw(GameTime gameTime)
        {
            for (int i = 0; i < msgText.Length; i++)
            {
                msgPos.X = (Screen.Width - smallFont.MeasureString(msgText[i]).X * Screen.Scale) * 0.5f;
                msgPos.Y = Screen.Height * 0.35f + smallFont.LineSpacing * Screen.Scale * 1.5f * i;

                SpriteBatch.DrawString(smallFont, msgText[i], msgPos, Color.White, 0, Vector2.Zero, Screen.Scale, SpriteEffects.None, 0);
            }
        }

        #endregion
    }

    #endregion
}
